package zzs.generalframework.office.core.excel.workbook;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

import org.apache.poi.hpsf.SummaryInformation;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import zzs.generalframework.office.common.constant.ExcelConstant;

/**
 * @title
 * @description
 * @project zzs-office
 * @package zzs.generalframework.office.core.excel.workbook
 * @author zzs
 * @date 2018年1月30日
 * @version 1.0
 */
public class WorkbookContext
{

	public Logger logger = LoggerFactory.getLogger(WorkbookContext.class);

	public Workbook createWorkBook(int type)
	{
		logger.info("====创建Workbook对象开始====");
		logger.debug("====创建Workbook对象,入参:int=[{}]====", type);
		Workbook workbook = null;
		if (type == ExcelConstant.XLS)
		{
			workbook = new HSSFWorkbook();
		}
		else
		{
			workbook = new SXSSFWorkbook(1000);
		}
		setAuthInfo(workbook);
		logger.debug("====创建Workbook对象,出参:Workbook=[{}]====", workbook);
		logger.info("====创建Workbook对象结束====");
		return workbook;
	}

	/**
	 * 获取Sheet页的个数
	 * 
	 * @param workbook
	 * @return
	 */
	public int getSheetSize(Workbook workbook)
	{
		logger.info("====获取Sheet数量开始====");
		logger.debug("====获取Sheet数量,入参:Workbook=[{}]====", workbook);
		int sheetSize = 0;
		sheetSize = workbook.getNumberOfSheets();
		logger.debug("====获取Sheet数量,出参:int=[{}]====", sheetSize);
		logger.info("====获取Sheet数量结束====");
		return sheetSize;
	}

	/**
	 * 获取workbook对象
	 * 
	 * @param type
	 * @param inputStream
	 * @return
	 * @throws IOException
	 */
	public Workbook getWorkbook(int type, InputStream inputStream) throws IOException
	{
		logger.info("====获取Workbook对象开始====");
		logger.debug("====获取Workbook对象,入参:int=[{}],InputStream=[{}]====", type, inputStream);
		Workbook workbook = null;
		if (ExcelConstant.XLS == type)
		{
			workbook = new HSSFWorkbook(inputStream);
		}
		else
		{
			workbook = new XSSFWorkbook(inputStream);
		}
		logger.debug("====获取Workbook对象,出参:Workbook=[{}]====", workbook);
		logger.info("====获取Workbook对象结束====");
		return workbook;

	}

	/**
	 * 设置信息
	 * 
	 * @param workbook
	 */
	private void setAuthInfo(Workbook workbook)
	{
		logger.info("==设置信息开始==");
		logger.debug("==设置信息,入参:Workbook=[{}]==", workbook);
		if (workbook instanceof HSSFWorkbook)
		{
			((HSSFWorkbook) workbook).createInformationProperties();
			((HSSFWorkbook) workbook).getDocumentSummaryInformation().setCompany("张仲升");
			SummaryInformation summaryInformation = ((HSSFWorkbook) workbook).getSummaryInformation();
			summaryInformation.setAuthor("张仲升");
			summaryInformation.setCreateDateTime(new Date());
			summaryInformation.setTitle("张仲升POI工具生成文件");
			summaryInformation.setKeywords("张仲升");
			summaryInformation.setLastAuthor("张仲升");
			summaryInformation.setLastSaveDateTime(new Date());
		}
		logger.info("==设置信息结束==");
	}
}
